home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / MacApp Documentation / MacApp AppleLink Messages / MacApp.Tech$ Oct 89 / Z0107-Meth Dispatch Detai-Oct89 < prev    next >
Encoding:
Text File  |  1989-10-17  |  5.8 KB  |  126 lines  |  [TEXT/GEOL]

  1. Item    2793331                         17-Oct-89        07:28
  2.  
  3. From:   D2079                           Arthur Anderson, M Lockwood,PRT
  4.  
  5. To:     MACAPP.TECH$                    MACAPP Tech
  6.  
  7. Sub:    Meth Dispatch Details
  8.  
  9. Jeff,
  10.  
  11. Most of the goodies used by the method dispatch mechanism are located in the
  12. %_MethTables CODE segment.
  13.  
  14. This segment contains a record which contains the name and size of a particular
  15. class.  The record format is as follows:
  16.  
  17.     ClassID     :   Integer;    {Stored in the first two bytes
  18.                                  of an instance}
  19.     ClassSize   :   Integer;    {Passed to NewHandle when creating
  20.                                  a new instance}
  21.     ClassName   :   MacsBugString;
  22.  
  23. The ClassName is in the format 'CLASINFO.TOBJECT'.  ClassIDs are always even
  24. numbers to allow for faster access to its super class in the super class table
  25. (described below).  TObject has ClassID = 2.  IfClassA is a subclass of ClassB,
  26. then ClassID(ClassA) > ClassID(ClassB).  This fact is important to the
  27. efficiency of the method dispatching mechanism.
  28.  
  29. The %_MethTables segment also contains a record for each method which contains
  30. references to the implementations of a method (for example, TView.Draw usually
  31. has several implementations).  This record has the following format:
  32.  
  33.     JSRInteger          : Integer = $4EBA;  {68000 JSR instruction}
  34.     MethDispatchOffset  : Integer;          {Offset to %_JMPTOTRAP}
  35.     ImplementationCount : Integer;          {# of implementations of this
  36.                                              method}
  37.     CachedClassID       : Integer;          {ClassID saved from last call
  38.                                              for this method}
  39.      CachedImplOffset    : Integer;         {Implementation saved from last
  40.                                              call for this method}
  41.     Array[1..# of implementations] of
  42.             ClassID     : Integer;          {ClassID for this implementation}
  43.             ImplOffset  : Integer;          {A5 relative jump table offset
  44.                                              for this implementation}
  45.  
  46. When you make a method call, you jump (through the jump table) to this record
  47. on the %_MethTables segment.  The first 4 bytes of the record are a JSR to
  48. %_JMPTOTRAP (See UObject.a). This contains a JMP to the method dispatch code.
  49. The method dispatch code then looks at the return address on the stack to
  50. get a pointer to the rest of the method table.
  51.  
  52. A handle to the object instance is pushed onto the stack right before
  53. the method call.  The method dispatch code looks at the ClassID (located
  54. in the first two bytes of an object instance) to determine which
  55. implementation to use.  It first looks for an implementation with
  56. the same ClassID as the object. If this is not found, it looks for
  57. implementations for a super class of the object (it uses the super class table,
  58. described below, for this).  To speed things up, the method table is ordered
  59. from most specific to most generic classes, and ClassIDs are ordered by
  60. inheritance as described above.  The ClassID and implementation for the most
  61. recent method call are cached to speed things up when you are making the same
  62. method call several times in a row.  See %_NEWMETHOD in UObject.a for more
  63. information.
  64.  
  65. The %_JMPTOTRAP trick is used because the location of the method dispatch code
  66. is not determined until run time.  The code is in the 128K and later ROMs,
  67. but must be in RAM for Macs with the 64K ROM.  In MacApp 2.0b9, a few
  68. instructions can be shaved off if the machine has a 68020 or 68030, and in that
  69. case the method dispatch code is also in RAM.
  70.  
  71. These records are tied together by the super class table and class info table.
  72. These are located at entry point %_CLASSINFO (look at a link map generated by
  73. the linker to find this).
  74.  
  75. The first two bytes contain the size of the super class table (including these
  76. two bytes).  Following this is an array of ClassIDs.  The ClassIDs are arranged
  77. so that you can compute the super class of a class by using the ClassID as an
  78. offset into the super class table (that's why ClassIDs are even).  For example,
  79. if TView has ClassID = 6, the ClassID of TEvtHandler will be located at offset
  80. 6 in the super class table.
  81.  
  82. Suppose:
  83.     ClassID(TObject) = 2
  84.     ClassID(TEvtHandler) = 4
  85.     ClassID(TView) = 6
  86.     ClassID(TWindow) = 8
  87.     ClassID(TControl) = 10
  88.     ClassID(TCtlMgr) = 12
  89.     ClassID(TDocument) = 14
  90.     ClassID(TApplication) = 16
  91.  
  92. Then the super class table would look like:
  93.  
  94. 18      {Size of table = 18}
  95. 0       {TObject has no super class}
  96. 2       {TEvtHandler = OBJECT (TObject)}
  97. 4       {TView = OBJECT (TEvtHandler)}
  98. 6       {TWindow = OBJECT (TView)}
  99. 6       {TControl = OBJECT (TView)}
  100. 10      {TCtlMgr = OBJECT (TControl)}
  101. 2       {TDocument = OBJECT (TEvtHandler)}
  102. 2       {TApplication = OBJECT (TEvtHandler)}
  103.  
  104. During run time, the location of the super class table is stored in the low
  105. memory global $BEC.
  106.  
  107. The class info table immediately follows the super class table. The first two
  108. bytes contain the size of the class info table (including these two bytes).
  109. Following this is an array of A5 relative offsets to jump table entries for the
  110. class info records described above.  When you call New(anObject), the ClassID
  111. for the object is passed to MakeNewInstance in UObject.Globals.p.  The ClassID
  112. is used as an offset into the class info table to find the jump table entry for
  113. the class info record for this class.  The size is passed to NewHandle, the
  114. ClassID is stored in the first two bytes of the object, and a new instance is
  115. created.
  116.  
  117. Last Note:  This information applies to 2.0b9, and earlier versions of MacApp
  118. with the -Opt option set.  It does not apply to the unoptimized dispatching
  119. mechanism used in 2.0b5 and earlier MacApps.
  120.  
  121. Sorry this is so long.  I hope it is of use or interest to fellow MacAppers.
  122.  
  123. Mike Lockwood
  124. Andersen Consulting
  125.  
  126.